/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameServlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import submarinesControl.SubmarinesControl;

/**
 *
 * @author Karnit Genchay
 */
@WebServlet(name = "GameServlet", urlPatterns = {"/game"})
public class GameServlet extends HttpServlet {
    // Name of the game in session
    public static final String gameSessionName = "GameDetails";
     SubmarinesControl sc;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
           response.setContentType("text/html;charset=UTF-8");

//           HttpSession currSession = request.getSession(true);

        if (getServletContext().getAttribute(gameSessionName) == null) {
            getServletContext().removeAttribute(gameSessionName);

            // Need to initiate this by the info recieved from the start page
            SubmarinesControl sc = new SubmarinesControl();
            getServletContext().setAttribute(gameSessionName, sc);
            sc.createNewGame(0, 0, true); 
            
            for (int i = 0; i < sc.getNumOfPlayers(); i++) {
                
            }
            response.sendRedirect("game");
        }
        else {
            sc = (SubmarinesControl)getServletContext().getAttribute(gameSessionName);
            
            if  (sc.isGameStarted()==false)
            {
//                sc.setGameStarted(true);
//                response.sendRedirect("game.jsp");
                   response.sendRedirect("waiting.jsp");
            }
            else{

                // Get the move info
                String strPlayer = request.getParameter("player");
                String strAtttack = request.getParameter("attack");
                String strPosition = request.getParameter("position");
                String strAction = request.getParameter("action");

                if ((strPlayer==null|| strAtttack==null||strPosition==null||strAction==null)){             
//                    currSession.removeAttribute(gameSessionName);
//                    sc = new SubmarinesControl();
//                    currSession.setAttribute(gameSessionName, sc);
                    response.sendRedirect("game.jsp");
                }
                else{
                    // Update the game info according to move
//                    SubmarinesControl scCurrSubmarines = 
//                        (SubmarinesControl)currSession.getAttribute(gameSessionName);
                       SubmarinesControl scCurrSubmarines = 
                        (SubmarinesControl) getServletContext().getAttribute(gameSessionName);
                    scCurrSubmarines.getGameManager().updateGame(strPlayer, strAtttack, strPosition, strAction );    
                    // Return to game page
                    response.sendRedirect("game.jsp");
                }
            }

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
